package com.cskaoyan.controller;

import com.cskaoyan.bean.data.GoodsDetailData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.data.PageData;
import com.cskaoyan.bean.vo.GoodsSearchVo;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.model.MarketCategory;
import com.cskaoyan.model.MarketGoods;
import com.cskaoyan.model.MarketSearchHistory;
import com.cskaoyan.service.CategoryService;
import com.cskaoyan.service.FootprintService;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.SearchHistoryService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author Pudding
 * @Date 2024/6/10 12:53
 */
@RestController
@RequestMapping("wx/goods")
public class WxGoodsController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    FootprintService footprintService;
    @Autowired
    SearchHistoryService searchHistoryService;

    //1. 首页信息中的商品总数信息
    @RequestMapping("count")
    public BaseRespVo count() {
        long count = goodsService.count();
        return BaseRespVo.ok(count);
    }

    //2. 商品信息详情：请求带有一个商品id
    @GetMapping("detail")
    public BaseRespVo detail(Integer id) {
        Integer userId = 0;

        //获取userId
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            userId = (Integer) subject.getPrincipal();

            //1. 每一次访问商品detail信息就会插入一条浏览记录
            footprintService.insert(userId, id);
        }
        //2. 获取数据
        GoodsDetailData data = goodsService.detail(id, userId);
        return BaseRespVo.ok(data);
    }

    //3. 商品详情下面的相关商品的推荐
    @GetMapping("related")
    public BaseRespVo related(Integer id) {
        //1. 根据商品id查询当前商品
        MarketGoods marketGoods = goodsService.findById(id);
        if (marketGoods == null) {
            return BaseRespVo.badArgumentValue();
        }

        //2. 该商品id存在，查找该商品对应的类目id
        Integer categoryId = marketGoods.getCategoryId();

        //3. 根据类目id，查询6个相关的产品，这里的limit=6、page = 1我们先写死
        int limit = 6;
        int page = 1;
        PageData data = goodsService.findRelatedGoodsByCategoryId(categoryId, page, limit);

        return BaseRespVo.ok(data);
    }

    //4. 商品类目信息的查询
    @GetMapping("category")
    public BaseRespVo category(Integer id) {
        //4.1 根据父类目id查询父类目信息
        MarketCategory parentCategory = categoryService.findById(id);

        //4.2 根据父类目id查询所有子类目信息
        List<MarketCategory> brotherCategory = categoryService.queryBrother(id);

        //4.3 根据父类目id查询所有子类目信息，选择子类目信息列表中的第一个
        MarketCategory currentCategory = categoryService.findCurrentCategory(id);

        //4.4 将上述信息封装到响应的data中，用一个map封装
        HashMap<String, Object> data = new HashMap<>();
        data.put("brotherCategory", brotherCategory);
        data.put("currentCategory", currentCategory);
        data.put("parentCategory", parentCategory);

        return BaseRespVo.ok(data);
    }

    //5. 商品列表信息查询 + 搜索商品信息列表
    @GetMapping("list")
    public BaseRespVo list(GoodsSearchVo goodsSearchVo) {
        //查询商品是不需要登录的
        List<MarketGoods> list = null;
        if (goodsSearchVo.getCategoryId() != null && goodsSearchVo.getCategoryId() != 0) {
            //查询data中的list信息，即商品类目id下的多条信息
            list = goodsService.list(goodsSearchVo.getCategoryId(), goodsSearchVo.getPage(), goodsSearchVo.getLimit());
        } else {
            list = goodsService.list(goodsSearchVo);
        }

        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            //获取userId
            if (subject.isAuthenticated()) {
                Integer userId = (Integer) subject.getPrincipal();
                if (goodsSearchVo.getKeyword() != null) {
                    //2.2 添加数据到market_search_history
                    searchHistoryService.insert(goodsSearchVo, userId);
                }
            }
        }
        //查询data中的filterCategoryList信息（这个信息是冗余的，没有用），所有我们就不用封装了
        PageData pageData = PageData.list(list);
        return BaseRespVo.ok(pageData);
    }
}
